package com.pk.platform.business.charge.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.pk.platform.business.charge.dao.IRefundDao;
import com.pk.platform.business.charge.service.IRefundService;
import com.pk.platform.business.charge.service.ITuitionDetailService;
import com.pk.platform.business.charge.vo.RefundVO;
import com.pk.platform.business.charge.vo.TuitionDetailVO;
import com.pk.platform.domain.charge.Refund;
import com.pk.platform.domain.charge.RefundDetail;
import com.pk.platform.domain.charge.Tuition;
import com.pk.platform.domain.charge.TuitionDetail;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class RefundServiceImpl implements IRefundService {

	private IRefundDao refundDao;
	
	private ITuitionDetailService tuitionDetailService;
	
	public void addRefund(Refund refund) {
		refundDao.add(refund);
		refundDao.addOperateLog("新增退费", Constant.SUCCESSFUL, refund.getKindergarten());
	}

	public void delRefund(String id) {
		Refund refund = this.queryRefundById(id);
		refundDao.addOperateLog("删除退费", Constant.SUCCESSFUL, refund.getKindergarten());
		Set<RefundDetail> rdSet = refund.getRefundDetailSet();
		int size = rdSet.size();
		String[] rdIds = new String[size];
		int i = 0;
		for(RefundDetail rd : rdSet)
		{
			rdIds[i] = rd.getId();
			i++;
		}
		this.delRefundDetail(rdIds);
		refundDao.getHibernateSession().flush();
		refundDao.delete(Refund.class, id);
	}

	public ListPage queryRefund(RefundVO rvo, Pager pager) {
		return refundDao.queryRefund(rvo, pager);
	}

	public Refund queryRefundById(String id) {
		return refundDao.get(Refund.class, id);
	}

	public void updateRefund(Refund refund) {
		Refund rf = this.queryRefundById(refund.getId());
		rf.setRemark(refund.getRemark());
		refundDao.addOperateLog("修改退费", Constant.SUCCESSFUL, rf.getKindergarten());
	}
	
	public ListPage queryRefundDetail(RefundVO rvo, Pager pager)
	{
		return refundDao.queryRefundDetail(rvo, pager);
	}
	
	public ListPage queryTuitionAndDetail(TuitionDetailVO tdvo, Pager pager)
	{
		return refundDao.queryTuitionAndDetail(tdvo, pager);
	}
	
	public synchronized void addRefundDetail(RefundDetail rd, Double maxAmount)
	{
		Double refundAmount = rd.getAmount();
		//减去相应的金额
		TuitionDetail td = tuitionDetailService.queryTuitionDetailById(rd.getTuitionDetail().getId());
		td.setAmount(maxAmount - refundAmount);
		refundDao.add(rd);
		
		//设置总金额
		Tuition tuition = td.getTuition();
		Double totalAmount = tuition.getTotalAmount();
		tuition.setTotalAmount(totalAmount - refundAmount);
		
		Refund refund = this.queryRefundById(rd.getRefund().getId());
		refundDao.addOperateLog("新增退费明细", Constant.SUCCESSFUL, refund.getKindergarten());
	}

	public void delRefundDetail(String[] ids)
	{
		for(String id : ids)
		{
			RefundDetail rd = this.queryRefundDetailById(id);
			refundDao.addOperateLog("删除退费明细", Constant.SUCCESSFUL, rd.getRefund().getKindergarten());
			//添加回退费金额
			TuitionDetail td = rd.getTuitionDetail();
			Double originalAmount = td.getAmount();
			td.setAmount(originalAmount + rd.getAmount());
			
			//设置总金额
			Tuition tuition = td.getTuition();
			Double totalAmount = tuition.getTotalAmount();
			tuition.setTotalAmount(totalAmount + rd.getAmount());
			
			refundDao.getHibernateSession().flush();
			refundDao.delete(RefundDetail.class, id);
		}
	}
	
	public RefundDetail queryRefundDetailById(String id)
	{
		return refundDao.get(RefundDetail.class, id);
	}
	
	public List<Map<String, Object>> queryRefundDetailList(String refundId)
	{
		return refundDao.queryRefundDetailList(refundId);
	}

	public IRefundDao getRefundDao() {
		return refundDao;
	}

	public void setRefundDao(IRefundDao refundDao) {
		this.refundDao = refundDao;
	}

	public ITuitionDetailService getTuitionDetailService() {
		return tuitionDetailService;
	}

	public void setTuitionDetailService(ITuitionDetailService tuitionDetailService) {
		this.tuitionDetailService = tuitionDetailService;
	}

}
